package com.jsu.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.jsu.bean.Found;
import com.jsu.bean.FoundDetail;
import com.jsu.bean.FoundMessage;
import com.jsu.bean.Lost;
import com.jsu.bean.LostDetail;
import com.jsu.bean.LostMessage;
import com.jsu.service.RecieveFoundService;
import com.jsu.service.RecieveLostService;
import com.jsu.service.UsrService;

@RestController
public class RecieveLostController {


	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmssSSS");
    String basicDir = "E:\\test\\";
    
    
   
    @Autowired
    UsrService usrService;
    @Autowired
    RecieveLostService recieveLostService;
    //上传寻人
	@RequestMapping(value = "/recieveLost", method = RequestMethod.POST)
	public void RecieveLost(MultipartHttpServletRequest request){  
		
	    System.out.println(request.getFile("photo"));
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("photo");
        System.out.println(files);
        for(MultipartFile file:files){
        	System.out.println("===================================");
        }
        Lost lost = new Lost();
        LostDetail lostDetail = new LostDetail();
        String username=request.getParameter("username");
        Integer id = usrService.getIdByName(username);
        if(id!=null){
        	lost.setPerson(id);
            lost.setNickname(request.getParameter("nickname"));
            lost.setLosttime(new Date());
            String lostposition=request.getParameter("lostposition");
            if(lostposition!=null&&(!lostposition.trim().equals(""))){
            	lost.setLostposition(lostposition);
            }
            lostDetail.setLost(lost);
            MultipartFile file = null;    
            BufferedOutputStream stream = null;    
            for (int i = 0; i < files.size(); ++i) { 
                file = files.get(i);    
                if (!file.isEmpty()) {    
                    try {
                    	java.util.Date dt = new Date();
                    	String sDateTime = sdf.format(dt);
                        byte[] bytes = file.getBytes();    
                        stream = new BufferedOutputStream(new FileOutputStream(    
                                new File(basicDir+sDateTime+".jpg"))); 
                        stream.write(bytes);    
                        stream.close();
                        LostMessage lostMessage = new LostMessage();
                        lostMessage.setProvider(id);
                        lostMessage.setPhotopath(sDateTime+".jpg");
                        lostMessage.setPhototime(dt);
                        lostDetail.addLostMessage(lostMessage);
                        for(int j=0;j<100000;j++);
                    } catch (Exception e) {    
                        stream = null;    
                        
                } 
            }    

    	}
            recieveLostService.recieveLost(lostDetail);
        }
        
}
}
